package prolog.builtins
import prolog.terms._

final case class is_builtin() extends FunBuiltin("is_builtin", 1) {

  override def exec(p: Prog) = {
    val a = getArg(0)
    
    a match {
      case _: ConstBuiltin => 1
      case _: FunBuiltin => 1
      case _ => 0
    }
  }
}




final case class fail_() extends ConstBuiltin("fail") {
  override def exec(p: Prog) = 0
}


final case class dif() extends FunBuiltin("dif", 3) {
  override def exec(p: Prog) = {
    val a: Num = getArg(0).asInstanceOf[Num]
    val b: Num = getArg(1).asInstanceOf[Num]
    val c: Real = Real(a.getValue - b.getValue)
    putArg(2, c, p)
  }
}

final case class listing() extends FunBuiltin("listing", 1) {

  def listpred(p: Prog, pred: Term): Int = {
    val cs = p.db.matches(List(pred))
    if (cs.eq(null)) 0
    else {
      cs.foreach(TermParser.printclause)
      1
    }
  }

  override def exec(p: Prog) = {
    val pred = getArg(0)
    if (pred.isInstanceOf[Var]) {
      p.db.foreach(x =>
        { x._2.foreach(TermParser.printclause); println })
      1
    } else
      listpred(p, pred)
  }
}

class true_ extends ConstBuiltin("true") {
  override def exec(p: Prog) = 1
}

